<?php

use think\migration\Migrator;
use think\migration\db\Column;

/**
 * 盲盒订单关联表迁移
 * 创建时间: 2025-10-29
 */
class OrderBlindBoxTable extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    public function change()
    {
        $this->createOrderBlindBoxTable();
    }
    
    /**
     * 创建盲盒订单关联表
     */
    private function createOrderBlindBoxTable()
    {
        $table = $this->table('sh_order_blind_box', [
            'engine' => 'InnoDB',
            'collation' => 'utf8mb4_general_ci',
            'comment' => '盲盒订单关联表'
        ]);
        
        $table->addColumn('uniacid', 'integer', [
                'default' => 0,
                'comment' => '站点ID'
            ])
            ->addColumn('blind_box_id', 'integer', [
                'comment' => '盲盒ID'
            ])
            ->addColumn('order_id', 'integer', [
                'comment' => '订单ID'
            ])
            ->addColumn('quantity', 'integer', [
                'default' => 1,
                'comment' => '购买数量'
            ])
            ->addColumn('is_drawn', 'boolean', [
                'default' => 0,
                'comment' => '是否已抽奖：0-未抽奖，1-已抽奖'
            ])
            ->addColumn('create_time', 'datetime', [
                'null' => true,
                'comment' => '创建时间'
            ])
            ->addIndex(['uniacid'], ['name' => 'idx_uniacid'])
            ->addIndex(['blind_box_id'], ['name' => 'idx_blind_box_id'])
            ->addIndex(['order_id'], ['name' => 'idx_order_id'])
            ->addIndex(['is_drawn'], ['name' => 'idx_is_drawn'])
            ->create();
    }
}
